#include "GameCore.h"
#include <SDL_opengl.h>
#include <gl\GLU.h>

#include "InputManager.h"
using namespace EPOC;


GameCore::GameCore(void)
{
}


GameCore::~GameCore(void)
{
}

void GameCore::Run()
{
	int result = Initialize();
	if(result <= 0)
	{
		/*TODO: Handle this. Something went wrong */
	}


	SDL_Event e;
	running = true;
	while(running)
	{
		while(SDL_PollEvent(&e)) 
		{
			OnEvent(&e);
		}
		// Fill with implemented helper/manager classes
		g_inputManager->Update(&e);



		// fill ends here
		Update();
		Render();
		
	}

	
	SDL_Quit();

}

int GameCore::Update()
{

	return 1;
}


void GameCore::OnEvent(SDL_Event* Event) 
{
	EpocEvent::OnEvent(Event);
}

void GameCore::OnExit() 
{
	running = false;
}

int GameCore::Initialize()
{
	SDL_Init(SDL_INIT_VIDEO);

	//SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
	displayWindow = SDL_CreateWindow("EPOC Origins", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, 800,600,  SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL/* | SDL_WINDOW_FULLSCREEN*/);
	if(displayWindow == nullptr)
	{
		//TODO: We got no window, use SDL_GetError();
	}

	displayRenderer = SDL_CreateRenderer(displayWindow, 1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
	if(displayRenderer == nullptr)
	{
		//TODO: We got no renderer, use SDL_GetError();
	}

	SDL_GetRendererInfo(displayRenderer, &displayRendererInfo); 

	/*TODO: Check that we have OpenGL */
	if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 || (displayRendererInfo.flags & SDL_RENDERER_TARGETTEXTURE) == 0) 
	{
		/*TODO: Handle this. We have no render surface and not accelerated. */
		_asm int 3;
	}


	InitOpenGL();
	SetViewport(800, 600);

	

	new InputManager();


	return true;
}

void GameCore::InitOpenGL()
{
	/* Enable smooth shading */
	glShadeModel( GL_SMOOTH );

	/* Set the background black */
	glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );

	/* Depth buffer setup */
	glClearDepth( 1.0f );

	/* Enables Depth Testing */
	glEnable( GL_DEPTH_TEST );

	/* The Type Of Depth Test To Do */
	glDepthFunc( GL_LEQUAL );

	/* Really Nice Perspective Calculations */
	glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
/* function to reset our viewport after a window resize */
int GameCore::SetViewport( int width, int height )
{
	/* Height / width ration */
	GLfloat ratio;

	/* Protect against a divide by zero */
	if ( height == 0 ) {
		height = 1;
	}

	ratio = ( GLfloat )width / ( GLfloat )height;

	/* Setup our viewport. */
	glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );

	/* change to the projection matrix and set our viewing volume. */
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );

	/* Set our perspective */
	gluPerspective( 45.0f, ratio, 0.1f, 100.0f );

	/* Make sure we're chaning the model view and not the projection */
	glMatrixMode( GL_MODELVIEW );

	/* Reset The View */
	glLoadIdentity( );

	return 1;
}

void GameCore::Render()
{
	
}

